import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from "@views/Home"
import DashBorad from "@views/DashBorad"

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "/home/dashborad",
    meta: {
      isNav: false,
      isLogin:true,
      jurisdiction:["1","2"]
    }
  }, 
  {
    path: '/home',
    name: '首页',
    redirect:'/home/dashborad' ,
    component: Home,
    meta: {
      isNav: false,
      isLogin:true,
      jurisdiction:["1","2"]
    },
    children: [
      
      {
        path: '/home/dashborad',
        name: '仪表盘',
        component: DashBorad,
        meta: {
          icon: "icon-bingtu",
          isNav: true,
          isLogin:true,
          jurisdiction:["1","2"]
        }
      },

      {
        path: '/home/datalists',
        name: '数据列表',
        meta: {
          icon: 'icon-liebiao',
          isNav: true,
          isLogin:true,
      jurisdiction:["1","2"]
        },
        component: () => import("@views/DataLists")
      },


      {
        path: '/home/msglists',
        name: '消息列表',
        meta: {
          icon: 'icon-buoumaotubiao14',
          isNav: true,
          isLogin:true,
      jurisdiction:["1","2"]
        },
        component: () => import("@views/MsgLists")
      },
      {
        path: '/home/setting',
        name: '设置',
        meta: {
          icon: 'icon-shezhi1',
          isNav: true,
          isLogin:true,
      jurisdiction:["1"]
        },
        component: () => import("@views/Setting")
      }, {
        path: '/home/datalists/dataedit',
        name: '编辑',
        meta: {

          isNav: false,
          isLogin:true,
      jurisdiction:["1","2"]
        },
        component: () => import("@views/DataEdit")
      }, {
        path: '/home/datalists/adddata',
        name: '添加数据',
        meta: {
          icon: "",
          isNav: false,
          isLogin:true,
      jurisdiction:["1","2"]
        },
        component: () => import("@views/AddData")
      },{
        path: '/home/notfound',
        name: '暂无数据',
        meta: {
          icon: "",
          isNav: false,
          isLogin:true,
          jurisdiction:["1","2"]
        },
        component: () => import("@views/NotFound")
      },{
        path: '*',
        name: '暂无数据',
        redirect:'/home/notfound',
        meta: {
          icon: "",
          isNav: false,
          isLogin:true,
          jurisdiction:["1","2"]
        },
      
      },
    ]
  }, {
    path: '/login',
    name: '登录页',
    component: () => import("@views/Login"),
    meta: {
      isNav: false,
      isLogin:false,
      jurisdiction:["1","2"]
    }

  },{
    path: '*',
    name: '暂无数据',
    redirect:"/notfound",
    meta: {
      isNav: false,
      isLogin:false,
      jurisdiction:["1","2"]
    }

  },,{
    path: '/notfound',
    name: '暂无数据',
    component: () => import("@views/NotFound"),
    meta: {
      isNav: false,
      isLogin:false,
      jurisdiction:["1","2"]
    }

  }
]

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}




const router = new VueRouter({
  mode: 'history',

  routes
})

router.beforeEach((to, from, next) => {
 
 
  if(to.meta.isLogin){
    if(!localStorage.getItem('token')){
     
      next('login')
    }else{
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      if(to.meta.jurisdiction.includes(userInfo.jurisdiction)){
         next()
      }else{
        
        next("/home/notfound")
      }
     
    }
  }else{
    next()
  }

 
 
    
 
  

})
export {
  routes,
  router
}
